//========encoding:utf-8=========
/*
 * @describe:  
 * @author:cjgong
 * @copyright:chinasoft.cn
 * @date:2020年1月16日 下午3:41:37 
 */

package edu.chinasoft.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import edu.chinasoft.pojo.Goods;
import edu.chinasoft.pojo.PageUtilForGoods;
import edu.chinasoft.util.JDBCTool;

public class GoodsDaoImp implements GoodsDao {
	private Logger logger = Logger.getLogger(GoodsDaoImp.class);

    /* (non-Javadoc)
	 * @see edu.chinasoft.dao.GoodsDao#insertTable(edu.chinasoft.pojo.Goods)
	 */
    @Override
	public boolean  insertTable(Goods goods)  {
    	logger.debug("GoodsDaoImp>>>insertTable  start ……");
    	
    	boolean result=false;
    	
    	Connection connection = JDBCTool.getConnection();
    	String sql="INSERT INTO t_goods (goods_name,goods_desc,goods_image,goods_price,goods_stock)  VALUES(?,?,?,?,?);";
    	PreparedStatement prepareStatement=null;
		try {
			prepareStatement = connection.prepareStatement(sql);
			prepareStatement.setString(1, goods.getGoodsName());
			prepareStatement.setString(2, goods.getGoodsDesc());
			prepareStatement.setString(3, goods.getGoodsImage());
			prepareStatement.setDouble(4, goods.getGoodsPrice());
			prepareStatement.setInt(5, goods.getGoodsStock());
			int num = prepareStatement.executeUpdate();
			
			if(num>0) {
				result=true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JDBCTool.closeJdbc(null, prepareStatement, connection);
		}
    	logger.debug("GoodsDaoImp>>>insertTable  end ……");
    	return result;
    }
    
    
	// 获取总记录数
	public PageUtilForGoods setRecordNum(PageUtilForGoods pageUtilForGoods) {
		logger.debug("GoodsDaoImp>>>setRecordNum  start ……");
		// 步骤1：从数据库获取总记录数
		Connection connection = JDBCTool.getConnection();
		String sql = "SELECT COUNT(*)  recordnum FROM t_goods;";
		PreparedStatement prepareStatement = null;
		ResultSet resultSet = null;
		try {
			prepareStatement = connection.prepareStatement(sql);
			resultSet = prepareStatement.executeQuery();
			if (resultSet.next()) {
				int recordNum = resultSet.getInt("recordnum");
				logger.debug("总记录数为：" + recordNum + "。");
				pageUtilForGoods.setRecordNum(recordNum);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCTool.closeJdbc(resultSet, prepareStatement, connection);
		}

		// 步骤2：把获取的记录数保存到pageUtilForUser
		logger.debug("GoodsDaoImp>>>setRecordNum  end ……");
		return pageUtilForGoods;
	}
	
	//获取分页
	@Override
	public PageUtilForGoods selectTablePage(int currentPage, int pageUtil) {
		logger.debug("GoodsDaoImp>>>selectTablePage  start ……");
		
		PageUtilForGoods pageUtilForGoods = new PageUtilForGoods();
		// 设置分页javabean 总记录数
		PageUtilForGoods setRecordNum = setRecordNum(pageUtilForGoods);
		// 设置分页单位
		pageUtilForGoods.setPageUtil(pageUtil);
		//设置当前页
		pageUtilForGoods.setCurrentPage(currentPage);
		
		
		// 查找分页里的记录
		Connection connection = JDBCTool.getConnection();
		String sql = "SELECT id,goods_name,goods_desc,goods_image,goods_price,goods_stock FROM t_goods  LIMIT ?,?;";
		PreparedStatement prepareStatement = null;
		ResultSet resultSet = null;
		try {
			prepareStatement = connection.prepareStatement(sql);
			prepareStatement.setInt(1, (currentPage - 1) * pageUtil);
			prepareStatement.setInt(2, pageUtil);
			resultSet = prepareStatement.executeQuery();

			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String goods_name = resultSet.getString("goods_name");
				String goods_desc = resultSet.getString("goods_desc");
				String goods_image = resultSet.getString("goods_image");
				double goods_price = resultSet.getDouble("goods_price");
				int goods_stock = resultSet.getInt("goods_stock");
				
				
				Goods goods = new Goods(id,goods_name,goods_desc,goods_image,goods_price,goods_stock);
				pageUtilForGoods.getGoodsList().add(goods);

			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCTool.closeJdbc(resultSet, prepareStatement, connection);
		}
		logger.debug("GoodsDaoImp>>>selectTablePage  end ……");
		return pageUtilForGoods;
	}
	//根据商品id查找商品的其他字段的值
	@Override
	public   Goods  selectGoodsById(Goods goods) {
		logger.debug("GoodsDaoImp>>>selectGoodsById  start ……");
		// 查找分页里的记录
		Goods result=null;
		Connection connection = JDBCTool.getConnection();
		String sql = "SELECT goods_name,goods_desc,goods_price,goods_stock FROM t_goods WHERE  id=?";
		PreparedStatement prepareStatement = null;
		ResultSet resultSet = null;
		try {
			prepareStatement = connection.prepareStatement(sql);

			prepareStatement.setInt(1, goods.getId());
			resultSet = prepareStatement.executeQuery();

			while (resultSet.next()) {
				
				String goods_name = resultSet.getString("goods_name");
				String goods_desc = resultSet.getString("goods_desc");
				double goods_price = resultSet.getDouble("goods_price");
				int goods_stock = resultSet.getInt("goods_stock");
				
				
				result= new Goods(goods.getId(),goods_name,goods_desc,goods_price,goods_stock);
			

			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCTool.closeJdbc(resultSet, prepareStatement, connection);
		}
		logger.debug("GoodsDaoImp>>>selectGoodsById  end ……");
		return result;
	}

}
